package com.walle.ak47.commons.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.walle.ak47.commons.redis.RedisContextHolder;

@Service
public class StintIPer
{
	@Autowired
	RedisContextHolder redisContextHolder ;
	
	private static final Logger LOG = LoggerFactory.getLogger(StintIPer.class);
	
	/**
	 * 
	 * @param ipAddress	ip地址
	 * @param cacheSeconds	限制时间范围（毫秒）
	 * @param stintCount	限制次数
	 * @return
	 */
	public boolean isEfficacious( String ipAddress, int cacheSeconds, int stintCount ) 
	{
        if (StringUtils.isNotBlank(ipAddress)) 
        {
            LOG.info("请求客户端的IP地址:" + ipAddress);
            long count = redisContextHolder.inrcMapValueToJedis( "StintIPs_count", ipAddress, 1, cacheSeconds) ;
            if ( StringUtils.isNotBlank(ipAddress) &&  count>stintCount )
            	 return false ;
            else
            	return true ;
        }
        else
        {
        	return false ;
        }
	}
	
}
